package com.example.boot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.pojo.entity.Inbound;
import com.example.pojo.entity.Outbound;
import com.example.service.IOutboundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author wy
 * @since 2024/7/18
 */

@RestController
@RequestMapping("/outbound")
@Api(tags = "物资出库接口")
@Slf4j
public class OutboundController {

    @Autowired
    private IOutboundService iOutboundService;

    /**
     * 分页查询
     */
    @ApiOperation("分页展示出库信息列表")
    @GetMapping("/selectPage")
    public Result selectPage(Outbound outbound,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Outbound> page = iOutboundService.selectPage(outbound, pageNum, pageSize);
        return Result.success(page);
    }

    @ApiOperation("物资出库")
    @PostMapping("/add")
    public Result add(@RequestBody Outbound outbound){
        iOutboundService.add(outbound);
        return Result.success(outbound);
    }

    @ApiOperation("修改出库信息")
    @PutMapping("/update")
    public Result update(@RequestBody Outbound outbound){
        iOutboundService.updateById(outbound);
        return Result.success();
    }

    @ApiOperation(("导出出库记录"))
    @GetMapping("/export")
    public Result export(HttpServletResponse response){
        List<Outbound> list = iOutboundService.export();
        ClassPathResource classPathResource = new ClassPathResource("/template/report_outbound.xlsx");

        try(
                // 读取模板文件创建Excel表格对象
                InputStream inputStream = classPathResource.getInputStream();
                XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
                // HttpServletResponse创建输出流
                ServletOutputStream out = response.getOutputStream();
        ){
            XSSFSheet sheet = workbook.getSheetAt(0);
            XSSFRow row = sheet.getRow(0);
            int rowNum = 1;
            for (Outbound outbound : list) {
                row = sheet.getRow(rowNum++);
                row.getCell(0).setCellValue(outbound.getWarehouseName());
                row.getCell(1).setCellValue(outbound.getMaterialName());
//                row.getCell(2).setCellValue(outbound.getOutboundTime());
                row.getCell(3).setCellValue(outbound.getOutboundQuantity());
                row.getCell(4).setCellValue(outbound.getProjectName());
                row.getCell(5).setCellValue(outbound.getProjectStage());
                row.getCell(6).setCellValue(outbound.getRemark());
            }


            //通过输出流进行文件下载
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=report.xlsx");
            workbook.write(out);
            out.flush();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return Result.success(list);
        }
    }
}
